<?php
/**
%%%copyright%%%
 *
 * FusionTicket - ticket reservation system
 * Copyright (C) 2007-2008 Christopher Jenkins. All rights reserved.
 *
 * Original Design:
 *	phpMyTicket - ticket reservation system
 * 	Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of fusionTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@noctem.co.uk if any conditions of this licencing isn't 
 * clear to you.
 */

?>
<?php

define("DB_DEADLOCK",1213);

class ShopDB {
		
  function init (){
    global $_SHOP;
    
    if(!isset($_SHOP->db) and isset($_SHOP->db_name)){
      $_SHOP->db = mysql_connect($_SHOP->db_host,$_SHOP->db_uname,$_SHOP->db_pass) 
      or die("Could not connect: ".mysql_error());

      mysql_select_db($_SHOP->db_name, $_SHOP->db) 
      or die("Could not find database: ".mysql_error());      
    }
  }
  
  
  function query ($query){
    global $_SHOP;
//echo  "QUERY: $query <br>";
    ShopDB::init();
    $res = mysql_query($query, $_SHOP->db);
    if(!$res){
      //echo mysql_error();
      user_error(mysql_error(),E_USER_ERROR);
      if(mysql_errno()==DB_DEADLOCK){
        $_SHOP->db_trx_started=FALSE;
      }
    }
    return $res;
  }
   
  function lock ($name,$time=30){
    $query_lock="SELECT GET_LOCK('SHOP_$name','$time')"; 
    if($res=ShopDB::query($query_lock) and $row=mysql_fetch_array($res)){
      return $row[0];
    }
  } 

  function unlock ($name){
    $query_lock="SELECT RELEASE_LOCK('SHOP_$name')"; 
    ShopDB::query($query_lock);
  } 
  
  function query_one_row ($query){
    if($result=ShopDB::query($query) and $row=mysql_fetch_array($result)){
      return $row;
    }
  }
  
  function quote ($s){
    return "'".mysql_real_escape_string($s)."'";
  }
  
  function begin (){
    global $_SHOP;
    if(!isset($_SHOP->db_trx_started)){
      if(ShopDB::query("begin")){
        $_SHOP->db_trx_started=TRUE;
		return TRUE;
      }else{
        user_error(mysql_error());
      }
    }else{
      return TRUE;
    }
  }
  
  function commit (){
    global $_SHOP;
    if($_SHOP->db_trx_started){
      if(ShopDB::query("commit")){
        $_SHOP->db_trx_started=FALSE;
		return TRUE;
      }else{
        user_error(mysql_error());
      }
    }
  }
  
  function rollback (){
    global $_SHOP;
    if($_SHOP->db_trx_started){
      if(ShopDB::query("rollback")){
        $_SHOP->db_trx_started=FALSE;
		return TRUE;
      }else{
        user_error(mysql_error());
      }
    }
  }
  
///
//new SQLi extenstions
	function initi (){
		global $_SHOP;
		
		if(!isset($_SHOP->link) and isset($_SHOP->db_name)){
			$_SHOP->link = mysqli_connect($_SHOP->db_host,$_SHOP->db_uname,$_SHOP->db_pass,$_SHOP->db_name)
			or die ("Could not connect: ".mysqli_connect_errno());
		}else{die ("No connection settings");}
	} 
	
//just requires PHP5 and MySQLi And mysql >4.1

  function begini (){
    global $_SHOP;
    if(!isset($_SHOP->db_trx_started)){
    	if(!$_SHOP->link){
    		ShopDB::initi();
    	}
      	mysqli_autocommit($_SHOP->link,false);
		$_SHOP->db_trx_started=TRUE;
		if(!$_SHOP->db_trx_started){
			return FALSE;
		}
		return TRUE;
    }else{
		return TRUE;
    }
  }
  function commiti (){
    global $_SHOP;
    if($_SHOP->db_trx_started){
    	if(mysqli_commit($_SHOP->link)){
			mysqli_autocommit($_SHOP->link,true);
        	$_SHOP->db_trx_started=FALSE;
        	unset($_SHOP->db_trx_started);
			return TRUE;	
		}else{
			user_error(mysqli_error($_SHOP->link));
		}
    }
  }
  function rollbacki (){
    global $_SHOP;
    if($_SHOP->db_trx_started){
    	if(mysqli_rollback($_SHOP->link)){
			mysqli_autocommit($_SHOP->link,true);
    		$_SHOP->db_trx_started=FALSE;
        	unset($_SHOP->db_trx_started);
			return TRUE;
		}else{
			user_error(mysqli_error($_SHOP->link));
		}
	}
  }
  

	function queryi($query){
		global $_SHOP;
		//echo  "QUERY: $query <br>";
		if(!$_SHOP->link){
    		ShopDB::initi();
    	}
    	$res = mysqli_query($_SHOP->link, $query);
    	
		if(!$res){
      		//echo mysql_error();
      		user_error(mysqli_error($_SHOP->link),E_USER_ERROR);
      		if(mysqli_errno()==DB_DEADLOCK){
        		$_SHOP->db_trx_started=FALSE;
      		}
    	}
    	return $res;	
	}
	
	function returnid(){
		global $_SHOP;
		if(!$_SHOP->link){
    		ShopDB::initi();
    	}
    	$id=mysqli_insert_id($_SHOP->link);
    	if($id){
			return $id;
		}else{return;}
	}
	
	function queryi_one_row ($query){
    	if($result=ShopDB::queryi($query) and $row=mysqli_fetch_array($result)){
      		return $row;
    	}
  	}
  	function quotei ($s){
  		global $_SHOP;
		  if(!isset($_SHOP->link)){
			ShopDB::initi();
		}
    	return "'".mysqli_real_escape_string($_SHOP->link,$s)."'";
  }

}
?>